package com.pai.gui;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
 /**
  * 功能：添加透明Frame组件的鼠标拖动功能
  * @author Vido
  *
  */
public class FrameListener extends MouseAdapter {
    private Point lastPoint = null;
    private TranslucentWindow window = null;
    
    
     /**
      * 功能：构造函数
      * @param window 要监听的窗口
      */
    public FrameListener(TranslucentWindow window){
        this.window = window;
    }
     
    
    /**
     * 功能：获取鼠标点击位置
     */
    @Override
    public void mousePressed(MouseEvent e){
    	//获取点击坐标
        lastPoint = e.getLocationOnScreen();
        //System.out.println(lastPoint);
    }
     
    
    /**
     * 功能：定义鼠标拖拽方法
     */
    @Override
    public void mouseDragged(MouseEvent e){
        //System.out.println(e);
    	//计算frame位置的偏移量
        Point point = e.getLocationOnScreen();
        int offsetX = point.x - lastPoint.x;
        int offsetY = point.y - lastPoint.y;
        
        //获取Frame的位置信息并添加偏移量
        Rectangle bounds = window.getBounds();
        bounds.x += offsetX;
        bounds.y += offsetY;
        
        //通过重设bounds修改frame的新位置
        window.setBounds(bounds);
        lastPoint = point;
    }
}